<template>
  <el-dialog
    @close="dkbjCancel('dkbjRuleForm')"
    :close-on-click-modal="false"
    title="对客报价"
    :visible.sync="dkbjVisible"
    width="900px"
  >
    <div class="scroll-view">
      <el-form
        :model="dkbjRuleForm"
        :rules="dkbjRules"
        ref="dkbjRuleForm"
        label-position="top"
      >
        <el-form-item label="报价金额" prop="old_amount">
          <el-input
            @input="inputChange"
            class="input-box"
            disabled
            v-model="dkbjRuleForm.old_amount"
            clearable
            v-input-limit="2"
          ></el-input>
        </el-form-item>
        <el-form-item label="预计签约金额" prop="new_amount">
          <el-input
            @input="inputChange"
            class="input-box"
            v-model="dkbjRuleForm.new_amount"
            clearable
            v-input-limit="2"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="优惠金额"
          prop="discount_amount"
          v-if="dkbjRuleForm.discount_amount"
        >
          <el-input
            disabled
            class="input-box"
            v-model="dkbjRuleForm.discount_amount"
            clearable
            v-input-limit="2"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            :rows="4"
            class="input-box"
            v-model="dkbjRuleForm.remark"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer">
      <div class="flex ac jb">
        <span class="tips" v-if="!showTip"></span>
        <el-button v-if="showTip" class="tips" type="text"
          >已超出优惠权限，需客户总监审批</el-button
        >
        <span>
          <el-button
            type="primary"
            @click="dkbjSubmit('dkbjRuleForm')"
            :disabled="diaLoading"
            :loading="diaLoading"
            >提交</el-button
          >
          <el-button @click="dkbjCancel('dkbjRuleForm')">关闭</el-button>
        </span>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import projectApi from "@/api/projectApi";
import companyApi from "@/api/companyApi";
import adminApi from "@/api/adminApi";
import { getUserInfo } from "@/utils/auth";
import foremanApi from "@/api/foremanApi";
import * as math from "mathjs";

export default {
  name: "",
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false,
      required: false,
    },
    project_id: {
      required: false,
    },
    zjData: {
      required: false,
    },
  },
  data() {
    return {
      dkbjVisible: false,
      dkbjRuleForm: {
        remark: "",
        old_amount: "",
        new_amount: "",
        discount_amount: "",
      },
      dkbjRules: {
        old_amount: [
          { required: true, message: "请输入原报价金额", trigger: "change" },
        ],
        new_amount: [
          { required: true, message: "请输入预计签约金额", trigger: "change" },
        ],
        discount_amount: [
          { required: true, message: "请输入优惠金额", trigger: "change" },
        ],
      },
      diaLoading: false,
      showTip: false,
    };
  },
  watch: {
    addDialogVisible(val1, val2) {
      if(this.zjData){
       this.dkbjRuleForm.old_amount =  this.zjData.quotation_amount
      }
      this.dkbjVisible = this.addDialogVisible;
    },
  },
  computed: {},
  mounted() {},
  methods: {
    inputChange() {
      let { discount_ratio } = getUserInfo();
      let dkbjRuleForm = this.dkbjRuleForm;
      let discount_amount = "";
      if (dkbjRuleForm.old_amount && dkbjRuleForm.new_amount) {
        discount_amount = Number(
          math.format(
            math.subtract(
              math.bignumber(dkbjRuleForm.old_amount),
              math.bignumber(dkbjRuleForm.new_amount)
            )
          )
        );
        dkbjRuleForm.discount_amount = discount_amount;
        if (discount_amount) {
          let bl = Number(
            math.format(
              math.divide(
                math.bignumber(discount_amount),
                math.bignumber(dkbjRuleForm.old_amount)
              )
            )
          );
          bl = bl * 100;
          this.showTip =
            bl > Number(discount_ratio) && bl != Number(discount_ratio);
        } else {
          this.showTip = false;
        }
      } else {
        this.showTip = false;
        dkbjRuleForm.discount_amount = "";
      }
      this.dkbjRuleForm = dkbjRuleForm;
    },
    // 对客报价
    dkbjCancel(formName) {
      this.dkbjRuleForm = {
        remark: "",
        old_amount: "",
        new_amount: "",
        discount_amount: "",
      };
      this.$nextTick(() => {
        this.$refs[formName].clearValidate();
        this.$emit("onClose", true);
      });
    },
    dkbjSubmit(formName) {
      let dkbjRuleForm = this.dkbjRuleForm;
      console.log(dkbjRuleForm, "对客报价参数");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.diaLoading = true;
          projectApi
            .toCustomerQuotation({
              project_id: this.project_id,
              remark: dkbjRuleForm.remark,
              old_amount: dkbjRuleForm.old_amount,
              new_amount: dkbjRuleForm.new_amount,
              discount_amount: dkbjRuleForm.discount_amount,
            })
            .then((res) => {
              console.log(res, "对客报价");
              this.diaLoading = false;
              if (res.code == 200) {
                this.$emit("onSuccess", true);
              }
              this.$message({
                type: res.code == 200 ? "success" : "warning",
                message: res.code == 200 ? "处理成功" : res.message,
              });
            })
            .catch((err) => {
              this.diaLoading = false;
              this.$message({
                type: "warning",
                message: err.message,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "./index.scss";
</style>